<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Admin;
use app\admin\model\CalendarEvent;
use think\exception\PDOException;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Schedule extends Backend
{

   /**
     * Schedule
     */
    protected $model = null;
    protected $childrenAdminIds = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Schedule');
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
    }

    /**
     * 列表
     */
    public function index()
    {
        //读取管理员列表
        $adminList = Admin::where('id', 'in', $this->childrenAdminIds)->field('id,nickname as name')->select();
        $this->assignconfig('admins', $adminList);

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }

            $start = $this->request->get('start');
            $end = $this->request->get('end');
            $type = $this->request->get('type');
            $admin_id = $this->request->get('admin_id');
            if (!$admin_id)
            {
                $admin_id = $type == 'my' ? $this->auth->id : 0;
            }
            $adminIds = $admin_id ? [$admin_id] : $this->childrenAdminIds;

            //读取日程事件列表
            $list = $this->model
                    ->where('teacherid', 'in', $adminIds)
                    ->where('starttime', 'between', [strtotime($start), strtotime($end)])
                    ->order('id desc')
                    ->select();
            $result = [];
            $time = time();
            foreach ($list as $k => $v)
            {
                $result[] = $v['render'];
            }
            return json($result);
        }
        //$eventList = CalendarEvent::where('admin_id', $this->auth->id)->order('id desc')->select();
        //$this->view->assign("eventList", $eventList);
        return $this->view->fetch();
    }

       /**
     * 详情
     */
    public function view($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));

        // $adminIds = $this->getDataLimitAdminIds();
        // if (is_array($adminIds))
        // {
        //     if (!in_array($row[$this->dataLimitField], $adminIds))
        //     {
        //         $this->error(__('You have no permission'));
        //     }
        // }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
